{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2006 by Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

const
 ServiceInterrupt = $A0;
 
 UIBase = $100;
 ApplicationBase = $200;
 PowerBase = $300;
 VFSBase = $400;
 ModuleBase = $500;
 HeapBase = $600;

{$i syscall.inc}

function OpenFile(filename: pchar; filemode: TFileFlags): THandle;
begin
   result := Syscall2(VFSBase+0, ptruint(filename), ptruint(filemode));
end;

procedure CloseFile(handle: THandle);
begin
   Syscall1(VFSBase+1, handle);
end;

function ReadFile(handle: THandle; buffer: Pointer; Size: int64): int64;
begin
   Syscall4(VFSBase+4, handle, ptruint(buffer), ptruint(@size), ptruint(@result));
end;

function WriteFile(handle: THandle; buffer: Pointer; Size: int64): int64;
begin
   Syscall4(VFSBase+5, handle, ptruint(buffer), ptruint(@size), ptruint(@result));
end;

function SeekFile(handle: THandle; Position: int64; SearchOffset: longword): int64;
begin
   Syscall4(VFSBase+3, handle, ptruint(@Position), SearchOffset, ptruint(@result));
end;

function FindFirst(searchBlock: PSearchBlock): longint;
begin
   //result := Syscall1(ptruint(searchBlock));
   result := 0;
end;

function FindNext(searchBlock: PSearchBlock): longint;
begin
   //result := Syscall1(ptruint(searchBlock));
   result := 0;
end;

function FindClose(searchBlock: PSearchBlock): longint;
begin
   result := 0;
end;

function FileSize(Handle: THandle): int64;
begin
   Syscall2(VFSBase+2, handle, ptruint(@result));
end;

function FileAge(Handle: THandle): TFileAge;
begin
   result := 0;
end;

function SetFileAge(Handle: THandle; NewAge: TFileAge): longint;
begin
   result := R_ERROR;
end;

function GetDir(buf: pchar; Len: PtrInt): PtrInt;
begin
   result := syscall2(VFSBase+6, ptruint(buf), len);
end;

function ChangeDir(Filename: pchar): PtrInt;
begin
   result := syscall1(VFSBase+7, ptruint(filename));
end;

function MakeDir(Filename: pchar): PtrInt;
begin
   result := syscall1(VFSBase+8, ptruint(filename));
end;

function RemoveDir(Filename: pchar): PtrInt;
begin
   result := syscall1(VFSBase+9, ptruint(filename));
end;


function CreateThread(Process: THandle; EntryPoint, Data: Pointer; StackSize: PtrUInt; ThreadPriority: TThreadPriority): THandle;
begin
   result := Syscall5(ApplicationBase+0, process, ptruint(entrypoint), ptruint(data), stacksize, ptruint(threadpriority));
end;

function DestroyThread(ThreadHandle: THandle): TResult;
begin
   result := syscall1(ApplicationBase+1, threadhandle);
end;


function SleepThread(ThreadHandle: THandle): TResult;
begin
   result := R_ERROR;
end;

function WakeupThread(ThreadHandle: THandle): TResult;
begin
   result := R_ERROR;
end;

function SetThreadPriority(ThreadHandle: THandle; NewPriority: TThreadPriority): TResult;
begin
   result := R_ERROR;
end;

function GetThreadPriority(ThreadHandle: THandle; NewPriority: PThreadPriority): TResult;
begin
   result := R_ERROR;
end;

function GetCurrentThreadHandle: THandle;
begin
   result := InvalidHandle;
end;

function GetStdInput: THandle;
begin
   result := syscall0(UIBase+0);
end;

function GetStdOutput: THandle;
begin
   result := syscall0(UIBase+1);
end;

function GetStdError: THandle;
begin
   result := syscall0(UIBase+2);
end;

function AllocateHeap: THandle;
begin
   result := syscall0(heapbase+0);
end;

procedure ReleaseHeap(H: THandle);
begin
   syscall1(heapbase+1, h);
end;

function HeapAlloc(Heap: THandle; Size: PtrInt): Pointer;
begin
   result := Pointer(Syscall2(heapbase+2, heap, size));
end;

procedure HeapFree(Heap: THandle; Ptr: Pointer);
begin
   Syscall1(heapbase+3, ptruint(ptr));
end;

function GetArgsize: PtrInt;
begin
   result := Syscall0(ApplicationBase+4);
end;

function GetArgs(Ptr: Pointer; Size: PtrInt): PtrInt;
begin
   result := Syscall2(ApplicationBase+5, PtrUInt(Ptr), size);
end;

function GetAppName(Ptr: Pointer; Size: PtrInt): PtrInt;
begin
   result := Syscall2(ApplicationBase+6, PtrUInt(Ptr), size);
end;